package com.ccc.netty.server.splitpacket.util;

import java.math.BigInteger;

/**
 * 十六进制转换工具类
 *
 * @author superC
 * @version 1.0.0
 * @date 2022/7/20
 */

public class MyHexUtils {

    public static String bytesToHexStr(byte[] bArray, int start, int len) {
        StringBuilder sb = new StringBuilder(bArray.length);
        String sTemp;
        for (int i = start; i < start + len; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static String byteToHexStr(byte b) {
        int i = 0xFF & b;
        return Integer.toHexString(0xFF & b);
    }

    public static String hexToDec(String hexStr) {
        BigInteger number = new BigInteger(hexStr, 16);
        return number.toString();
    }

    public static String asciiToStr(byte[] arr) {
        StringBuilder sb = new StringBuilder();
        for (byte b : arr) {
            sb.append((char) b);
        }
        return sb.toString();
    }

}